/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package SynchronizationLayer;

import DataModel.NotaryCertificate;
import java.util.ArrayList;
import Repository.IRepositoryProxy;

/**
 *
 * @author Tomasz Nowak
 */
public class Conversation
{
	ConversationGroup conversationGroup;
	NotaryCertificate certificate;
	ArrayList<ExpectedPacket> expectedPackets;
	IPacketHandler packetHandler;
	Thread conversationThread;
	ConversationRunner runner;

	public ConversationGroup getConversationGroup()
	{
		return conversationGroup;
	}

	public void setConversationGroup( ConversationGroup conversationGroup )
	{
		this.conversationGroup = conversationGroup;
	}

	public Conversation( NotaryCertificate certificate, ArrayList<ExpectedPacket> expectedPackets, IPacketHandler handler )
	{
		this.certificate = certificate;
		this.expectedPackets = expectedPackets;
		this.packetHandler = handler;
	}

	public NotaryCertificate getCertificate()
	{
		return certificate;
	}

	public void setCertificate( NotaryCertificate certificate )
	{
		this.certificate = certificate;
	}

	public boolean expect( ExpectedPacket expectedPacket )
	{
		return expectedPackets.contains(expectedPacket);
	}

	public void receivePacket( NotarySynchronizationPacket receivedPacket, IRepositoryProxy repositoryProxy, ISynchronizationServer synchronizationServer )
	{
		runner = new ConversationRunner(receivedPacket, repositoryProxy, synchronizationServer,
																		expectedPackets, packetHandler);
		SynchronizationExecutor.getInstance().getCachedThreadPool().execute(runner);
	}

	void raiseTimeoutHandler()
	{
		runner.handleTimeout();
		SynchronizationExecutor.getInstance().getCachedThreadPool().execute(runner);

		conversationGroup.removeConversation(this);
	}
}
